﻿using DCode.EmailValidator.Core.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace DCode.EmailValidator.UI.Tools
{
    /// <summary>
    /// Summary description for EmailService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class EmailService : System.Web.Services.WebService
    {

        [WebMethod]
        public ValidatorResult ValidateAddress(string login, string password, string email)
        {
            ValidatorResult response = null;

            try
            {
                if ((login ?? "") == "" || (password ?? "") == "")
                {
                    response = new ValidatorResult() { IsValid = false, Message = "Login or password are required", ResultType = ResponseType.NoAvailableTransactions };
                    return response;
                }

                if (DCode.EmailValidator.UI.Domain.User.CheckUser(login, password))
                {
                    if (DCode.EmailValidator.UI.Domain.User.GetRemainTransactions(0) > 0)
                    {
                        using (var validator = new Core.Validator())
                        {
                            response = validator.ValidateMail("info@d-code.cl", email);
                        }
                    }
                    else
                    {
                        response = new ValidatorResult() { IsValid = false, Message = "Login or password incorrect", ResultType = ResponseType.NoAvailableTransactions };
                    }
                }
                else
                {
                    response = new ValidatorResult() { IsValid = false, Message = "Login or password incorrect", ResultType = ResponseType.Error};
                }
            }
            catch (Exception ex)
            {
                response = new ValidatorResult() { IsValid = false, Message = "Internal error please retry later: " + ex.Message, ResultType = ResponseType.Error };
            }

            return response;
        }
    }
}
